﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Niagara.Core;
using System.Reflection;
using System.IO;
using System.ComponentModel.DataAnnotations;

namespace Niagara.UnitTests
{
  /// <summary>
  /// Summary description for ProxyGeneratorTests
  /// </summary>
  [TestClass]
  public class ReflectionMetadataProviderTests
  {
    public ReflectionMetadataProviderTests()
    {
      //
      // TODO: Add constructor logic here
      //
    }

    private TestContext testContextInstance;
    ReflectionAttributeMetadataProvider provider;
    static Assembly metadataAssembly;

    /// <summary>
    ///Gets or sets the test context which provides
    ///information about and functionality for the current test run.
    ///</summary>
    public TestContext TestContext
    {
      get
      {
        return testContextInstance;
      }
      set
      {
        testContextInstance = value;
      }
    }

    [ClassInitialize]
    public static void ClassSetup(TestContext context)
    {
      metadataAssembly = Assembly.LoadFile(string.Concat(Directory.GetCurrentDirectory(), "\\XBoxGames.Data.dll"));
    }
    
    [TestInitialize]
    public void TestSetup()
    {
      provider = new ReflectionAttributeMetadataProvider(metadataAssembly);
    }

    [TestCleanup]
    public void TestTeardown()
    {
      provider = null;
    }

    [TestMethod]
    [Description("Test Retrieval of attributes for Types")]
    public void TestTypeAttributes()
    {
      var results = provider.GetTypeAttributes("XBoxGames.Game");
      Assert.IsTrue(results.Count() == 0);
    }

    [TestMethod]
    [Description("Test Retrieval of attributes for Properties")]
    public void TestPropertyAttributes()
    {
      var results = provider.GetPropertyAttributes("XBoxGames.Game", "Name");
      Assert.IsTrue(results.Count() == 2, "Number of Name Attributes should be two.");
      var attributes = results.ToArray();
      Assert.IsInstanceOfType(attributes[0], typeof(StringLengthAttribute), "First attribute should be Required");
      Assert.IsTrue(((StringLengthAttribute)attributes[0]).MaximumLength == 100, "String Length should be 100.");
      Assert.IsInstanceOfType(attributes[1], typeof(RequiredAttribute), "First attribute should be Required");
    }
  }
}
